Release 10.1A: OpenEdge Development:
Progress 4GL Handbook


Creating a dynamic temp-table

Next, look at a few parts of procedure h-gettemptable.p.

The main block creates a dynamic query for the database tables requested (such as Order and Customer). The createBuffers procedure creates a dynamic buffer for each of those tables.

The getFieldHandles procedure assembles a list of the handles to the fields requested (such as Order.OrderNum).

The createTempTable procedure creates a dynamic temp-table called hTT, and adds iCount to it as its first field:

 /* Create the temp table */ 
  CREATE TEMP-TABLE hTT. 
 /* Add a count field - we need this for the sequence so that we get the records 
    back in the order that they were created */ 
  hTT:ADD-NEW-FIELD("iCount","INTEGER"). 

It then walks through the list of database field handles and adds a field to the temp-table like each field:

 /* Iterate through the list of field handles in the cTableFields */ 
  DO iCount = 1 TO NUM-ENTRIES(cTableFields): 
    /* Convert the current entry to a handle */ 
    hField = WIDGET-HANDLE(ENTRY(iCount,cTableFields)). 
    /* Add a field to the temp table like this field with the same name as this  
      field */ 
    hTT:ADD-LIKE-FIELD(hField:NAME,hField). 
  END. 

It then adds an index on iCount, so that the records remain in the order they were created:

 /* Add a primary unique index to the temp-table */ 
  hTT:ADD-NEW-INDEX("pudx",True,True). 
   
  /* Add the counter field to the index */ 
  hTT:ADD-INDEX-FIELD("pudx","iCount"). 

Finally, it prepares the temp-table, which freezes its definition and enables you to add records to it:

  /* Prepare the temp-table with the name the user chose */ 
   hTT:TEMP-TABLE-PREPARE(icTableName). 

Next, the h-gettemptable.p procedure populates the temp-table in the populateTempTable internal procedure. Handle hTTBuf holds the handle to the temp-table’s default buffer:

  /* Store the handle to the buffer for the temp-table */ 
   hTTBuff = hTT:DEFAULT-BUFFER-HANDLE. 

The procedure prepares the database query with the FOR EACH statement entered in the dialog box, opens it, and retrieves the first record:

  /* Prepare a query using the string the user provided */ 
  hQuery:QUERY-PREPARE(icForEach). 
  /* Open the query */ 
  hQuery:QUERY-OPEN(). 
  /* Get the first result in the result set */ 
  hQuery:GET-FIRST(). 

For as many rows as there are in the query, or as many rows as you asked for, whichever is less, the procedure creates a temp-table row using the buffer handle:

  /* Iterate for as many rows as the user has chosen, or until the query is  
     off-end */ 
  REPEAT iCount = 1 TO iiRows WHILE NOT hQuery:QUERY-OFF-END: 
    
    /* Create a temp-table record */ 
    hTTBuff:BUFFER-CREATE(). 

Because you selected individual fields from possibly multiple tables in the database to add to the temp-table, it doesn’t do a BUFFER-COPY to move fields from the database records to the newly created temp-table row. Instead, the next block of code uses the BUFFER-FIELD and BUFFER-VALUE attributes on both the database buffer fields and the temp-table buffer fields to move values from one to the other.

The procedure then releases each temp-table row, iterates through the query until the REPEAT condition fails, and then closes the query:

 /* Release the temp-table record */ 
    hTTBuff:BUFFER-RELEASE(). 
    /* Get teh next result in the query */ 
    hQuery:GET-NEXT(). 
  END. 
  /* Close the query */ 
  hQuery:QUERY-CLOSE(). 


Copyright © 2005 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095